/**
* User.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {

	attributes: {
		name: {
			type: 'string',
			required: true,
			//unique: true
		},

		email: {
			type: 'email',
			unique: true,
			required: true,
		},

		encryptedPassword: {
			type: 'string',
			required: true
		},

		lastLoggedIn: {
			type: 'date',
			required: true,
			defaultsTo: new Date(0)
		},

		gravatarUrl: {
      		type: 'string'
      	},

		quizzes: {
			collection: 'quiz',
			via: 'user'
		},

		profileImage: {
			collection: 'avatarImage',
			via: 'user'
		}
	},



	beforeCreate: function(values, cb) {
		//console.log( User.findOne({email: values.email}) )
		User.findOne({'email': values.email}).exec(function (err, record) {
			if (record){
				console.log('We found ' + record)
				cb({'error': 'duplicate entry'});
			}
			else {
				//console.log(cb())
				cb();
			}
		});
	}
	/*

	beforeCreate: function(attrs, next) {
		var bcrypt = require('bcrypt');

		bcrypt.genSalt(10, function (err, salt) {
			if (err) return next(err);

			bcrypt.hash(attrs.password, salt, function (err, hash) {
				if (err) return next(err);

				attrs.password = hash;
				next();
			});
		});
	}
	*/
};

